Go back to the Preprocessing page. This link might be useful to keep track of the files created during the preprocessing.

Let us set some global options for all code chunks in this document.

knitr::opts_chunk$set(
  message = FALSE,    # Disable messages printed by R code chunks
  warning = FALSE,    # Disable warnings printed by R code chunks
  echo = TRUE,        # Show R code within code chunks in output
  include = TRUE,     # Include both R code and its results in output
  eval = TRUE,       # Evaluate R code chunks
  cache = FALSE,       # Enable caching of R code chunks for faster rendering
  fig.align = "center",
  out.width = "100%",
  retina = 2,
  error = TRUE,
  collapse = FALSE
)
rm(list = ls())
set.seed(1982)

1 Import libraries

library(sf)
library(jsonlite)
library(dplyr)

library(here)
library(rmarkdown)
library(listviewer) # to use jsonedit()

rm(list = ls()) # Clear the workspace
set.seed(1982) # Set seed for reproducibility

2 Load the data

# read shape file
tomtom_shp <- st_read(here("data_files/San_Francisco_data_from_TomTom/network.shp"), quiet = TRUE)

# read json file and select the information of interest
raw_json <- fromJSON(here("data_files/San_Francisco_data_from_TomTom.json"))

2.1 Explore the data

# Displaying the structure of the nested list
jsonedit(raw_json)
tomtom_shp |> head(5) |> paged_table()
tomtom_shp |> dim()
## [1] 67199     8

3 Extract the relevant data

# Extract the relevant data from the JSON file
tomtom_json <- raw_json$network$segmentResults$segmentTimeResults
# Initialize empty data frames for 10 and 12 columns
df_10_columns <- data.frame()
df_12_columns <- data.frame()

# Iterate through the list of data frames
for (i in seq_along(tomtom_json)) {
  # Check the number of columns in the current data frame
  num_cols <- ncol(tomtom_json[[i]])
  
  # Add list number column
  tomtom_json[[i]]$List_Number <- i
  
  # Append to the appropriate data frame based on the number of columns
  if (num_cols == 10) {
    df_10_columns <- rbind(df_10_columns, tomtom_json[[i]])
  } else if (num_cols == 12) {
    df_12_columns <- rbind(df_12_columns, tomtom_json[[i]])
  }
}

# add two columns with NA values so that we can rbin later
from_10_to_12 = df_10_columns %>% mutate(standardDeviationSpeed = NA, travelTimeStandardDeviation = NA)
  
# rbind and  order by List_Number
almost_tomtom = rbind(from_10_to_12, df_12_columns) %>% arrange(List_Number)

# join shape file and the dataset we get from the above process
casi_tomtom = bind_cols(tomtom_shp, almost_tomtom) %>% 
  mutate(FRC = as.character(FRC))

PERCENTILES = do.call(rbind, casi_tomtom$speedPercentiles) %>% as.data.frame()
names(PERCENTILES) =  paste(seq(5, 95, by = 5), "percentile", sep = "")

tomtom = bind_cols(casi_tomtom, PERCENTILES) %>% dplyr::select(-speedPercentiles)


# save the obtained data set
save(tomtom, file = here("data_files/tomtom.RData"))

3.1 Explore the data

tomtom |> head(5) |> paged_table()
tomtom |> dim()
## [1] 67199    39

4 Description of the Functional Road Classes (FRC) values

The Functional Road Classes (FRC) values are used to classify roads based on their importance and usage. The table below (taken from this page) provides a description of the different FRC values. We remark this classification because later this will determine the size of the graph.

# Load necessary library
library(knitr)

# Create the table data
table_data <- data.frame(
  `FRC VALUE` = c(0, 1, 2, 3, 4, 5, 6, 7, 8),
  `Short Description` = c(
    "Motorways; Freeways; Major Roads",
    "Major Roads less important than Motorways",
    "Other Major Roads",
    "Secondary Roads",
    "Local Connecting Roads",
    "Local Roads of High Importance",
    "Local Roads",
    "Local Roads of Minor Importance",
    "Other Roads"
  ),
  `Long Description` = c(
    "All roads that are officially assigned as motorways.",
    "All roads of high importance, but not officially assigned as motorways, that are part of a connection used for international and national traffic and transport.",
    "All roads used to travel between different neighboring regions of a country.",
    "All roads used to travel between different parts of the same region.",
    "All roads making all settlements accessible or making parts (north, south, east, west, and central) of a settlement accessible.",
    "All local roads that are the main connections in a settlement. These are the roads where important through traffic is possible e.g.,: • arterial roads within suburban areas, industrial areas or residential areas, • a rural road, which has the sole function of connecting to a national park or important tourist attraction.",
    "All roads used to travel within a part of a settlement or roads of minor connecting importance in a rural area.",
    "All roads that only have a destination function, e.g., dead-end roads, roads inside a living area, alleys: narrow roads between buildings, in a park or garden.",
    "All other roads that are less important for a navigation system: • a path: a road that is too small to be driven by a passenger car, • bicycle paths or footpaths that are especially designed as such, • stairs, • pedestrian tunnel, • pedestrian bridge, • alleys that are too small to be driven by a passenger car."
  )
)

# Print the table
kable(table_data, align = "cll", 
      caption = "Description of the Functional Road Classes (FRC) values.", 
      col.names = c("FRC VALUE", "Short Description", "Long Description"))
Description of the Functional Road Classes (FRC) values.
FRC VALUE Short Description Long Description
0 Motorways; Freeways; Major Roads All roads that are officially assigned as motorways.
1 Major Roads less important than Motorways All roads of high importance, but not officially assigned as motorways, that are part of a connection used for international and national traffic and transport.
2 Other Major Roads All roads used to travel between different neighboring regions of a country.
3 Secondary Roads All roads used to travel between different parts of the same region.
4 Local Connecting Roads All roads making all settlements accessible or making parts (north, south, east, west, and central) of a settlement accessible.
5 Local Roads of High Importance All local roads that are the main connections in a settlement. These are the roads where important through traffic is possible e.g.,: • arterial roads within suburban areas, industrial areas or residential areas, • a rural road, which has the sole function of connecting to a national park or important tourist attraction.
6 Local Roads All roads used to travel within a part of a settlement or roads of minor connecting importance in a rural area.
7 Local Roads of Minor Importance All roads that only have a destination function, e.g., dead-end roads, roads inside a living area, alleys: narrow roads between buildings, in a park or garden.
8 Other Roads All other roads that are less important for a navigation system: • a path: a road that is too small to be driven by a passenger car, • bicycle paths or footpaths that are especially designed as such, • stairs, • pedestrian tunnel, • pedestrian bridge, • alleys that are too small to be driven by a passenger car.